﻿using System;
using System.Web;

namespace FredCK.FCKeditorV2.ImageUpload
{
    /// <summary>
    /// 图片上传的抽象基类
    /// </summary>
    public abstract class Base : System.Web.UI.Page
    {
        private System.Web.HttpPostedFile mPostFile;

        /// <summary>
        /// 上传文件
        /// </summary>
        public System.Web.HttpPostedFile PostFile {
            get { return this.mPostFile; }
            set { this.mPostFile = value; }
        }

        /// <summary>
        /// 文件上传结束回调客户端
        /// </summary>
        /// <param name="isSucceed">上传是否成功</param>
        /// <param name="fileUrl">上传完成后该文件的URL</param>
        /// <param name="customMsg">自定义消息</param>
        public static void SendFileUploadResponse(bool isSucceed, string fileUrl, string customMsg)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Write("<script type='text/javascript'>");
            System.Web.HttpContext.Current.Response.Write(@"(function(){var d=document.domain;while (true){try{var A=window.top.opener.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\.|$)/,'');if (d.length==0) break;try{}catch (e){break;}}})();");
            System.Web.HttpContext.Current.Response.Write("window.parent.OnUploadCompleted(" + isSucceed.ToString().ToLower() + ", '" + fileUrl + "', '" + customMsg + "');");
            System.Web.HttpContext.Current.Response.Write("</script>");
            System.Web.HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 图片文件处理及保存
        /// 无论结果如何，类的逻辑终点处必需调用 SendFileUploadResponse 以响应FCK客户端
        /// </summary>
        public abstract void Save();

    }//end class
}
